package com.cooper.service.project;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.dao.account.ShopCartDAO;
import com.cooper.entity.ShopCartEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.vo.CompositeShopCartItemVo;

@Service
public class ShopCartService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ShopCartDAO shopCartDao;
	
	public void createShopCart(ShopCartEntity shopCart){
		try {
			if(shopCart!=null&&shopCart.getItemId()!=null){
				CompositeShopCartItemVo queryParamVo = new CompositeShopCartItemVo();
				queryParamVo.setCookie(shopCart.getCookie());
				queryParamVo.setUserId(shopCart.getUserId());
				queryParamVo.setItemId(shopCart.getItemId());
				queryParamVo.setSkuId(shopCart.getSkuId());
				PageResult<CompositeShopCartItemVo> ret = this.findCompositeShopCarts(queryParamVo, new PageInfo());
				if(ret!=null&&ret.getResult()!=null&&ret.getResult().size()>0){
					Long id = ret.getResult().get(0).getId();
					ShopCartEntity cartEntity = shopCartDao.getShopCartDo(id);
					cartEntity.setGmtModified(shopCart.getGmtModified());
					cartEntity.setModifier(shopCart.getModifier());
					cartEntity.setQuantity(cartEntity.getQuantity()+shopCart.getQuantity());
					shopCartDao.updateShopCart(cartEntity);
				} else {
					shopCartDao.addShopCart(shopCart);
				}
			}
			
		} catch (Exception e) {
			logger.error("ShopCartService-createShopCart-error", e);
		}
	}
	
	public void updateShopCart(ShopCartEntity shopCartDo){
		try {
			shopCartDao.updateShopCart(shopCartDo);
		} catch (Exception e) {
			logger.error("ShopCartService-updateShopCart-error", e);
		}
	}
	
	public void updateUserIdByCookie(Long userId , String cookie){
		try {
			shopCartDao.updateUserIdByCookie(userId, cookie);
		} catch (Exception e) {
			logger.error("ShopCartService-removeShopCart-error", e);
		}
	}
	
	public void removeShopCart(Long id){
		try {
			shopCartDao.deleteShopCart(id);
		} catch (Exception e) {
			logger.error("ShopCartService-removeShopCart-error", e);
		}
	}
	
	public void removeShopCart(List<Long> ids){
		try {
			shopCartDao.deleteShopCart(ids);
		} catch (Exception e) {
			logger.error("ShopCartService-removeShopCart-error", e);
		}
	}
	
	public ShopCartEntity findShopCartById(Long id){
		try {
			return shopCartDao.getShopCartDo(id);
		} catch (Exception e) {
			logger.error("ShopCartService-findShopCartById-error", e);
		}
		return null;
	}
	
	public CompositeShopCartItemVo getCompositeShopCart(long itemId, long skuId) throws Exception{
		return this.shopCartDao.getCompositeShopCart(itemId, skuId);
	}
	
	@SuppressWarnings("unchecked")
	public PageResult<CompositeShopCartItemVo> findCompositeShopCarts(CompositeShopCartItemVo queryParamVo, PageInfo pageInfo){
		int rowCount = this.shopCartDao.findCompositeShopCartItemVoCount(queryParamVo);
		pageInfo.setRowCount(rowCount);
		if(0 == rowCount){
			return new PageResult<CompositeShopCartItemVo>(Collections.EMPTY_LIST, pageInfo);
		}
		List<CompositeShopCartItemVo> dataList = this.shopCartDao.findCompositeShopCartItemVo(queryParamVo, pageInfo);
		return new PageResult<CompositeShopCartItemVo>(dataList, pageInfo);
	}
}
